<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class Cors
{
    public function handle(Request $request, Closure $next)
    {
        $response = $next($request);
        if ($response instanceof BinaryFileResponse) {
            return $response;
        }
        //$response->header('Content-Type', 'application/json;charset=utf-8');
        if($request->ajax()){
            $response->header('Access-Control-Allow-Origin', '*');
            $response->header('Access-Control-Allow-Methods', 'POST,GET,PUT,OPTIONS,DELETE');// 允许请求的类型
            $response->header('Access-Control-Allow-Credentials', 'false');// 设置是否允许发送 cookies
            $response->header('Access-Control-Allow-Headers', '*');
        }
        // 设置允
        return $response;
    }
}
